package com.zcqk.config;

import java.io.IOException;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 配置Spring的扫描路径
 * 
 * @author CrazyShaQiuShi
 *
 */
@Configuration
@PropertySource({ "classpath:/db.properties" })
@ComponentScans(@ComponentScan(basePackages = { "com" }, excludeFilters = {
		@Filter(type = FilterType.ANNOTATION, classes = Controller.class) }))
@EnableTransactionManagement
public class SpringApplicationContextConfig {

	@Autowired
	private Environment environment;

	@Bean("dataSource")
	public DataSource getDruidDataSource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUsername(environment.getProperty("jdbc.username"));
		dataSource.setPassword(environment.getProperty("jdbc.password"));
		dataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
		dataSource.setUrl(environment.getProperty("jdbc.url"));
		return dataSource;
	}

	@Autowired(required=false)
	private DataSource dataSource;

	// 配置Mybatis的SqlSessionFactory
	@Bean("sqlSessionFactory")
	public SqlSessionFactoryBean getSqlSessionFactoryBean() throws IOException {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		Resource configLocation = new ClassPathResource("mybatis-configuration.xml");
		sqlSessionFactoryBean.setConfigLocation(configLocation);
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource[] mapperLocations = resolver.getResources("classpath:mapper/*-mapper.xml");
		sqlSessionFactoryBean.setMapperLocations(mapperLocations);
		return sqlSessionFactoryBean;
	}

	/**
	 * 事物管理
	 * 
	 * @return
	 */
	@Bean("transactionManager")
	public PlatformTransactionManager getDataSourceTransactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSource);
		return transactionManager;
	}
}
